﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BaseSet.Data;
using BaseSet.Logic;
using BaseSet.Entity;
using BaseSet.Framework;
using BaseSet.Entity.Common;
using BaseSet.Entity.Common.Text;
using BaseSet;
using System.Data;

namespace WebSite.Admin
{
    public partial class Login : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        #region 登录
        protected void ibSubmit_Click(object sender, ImageClickEventArgs e)
        {
            UsersHelper uHelper = new UsersHelper();

            string username = txtUserName.Text.Trim();
            string password = txtPassword.Text.Trim();

            Criteria c = new Criteria();
            c.Add(CriteriaType.Equals, "UserName", username);
            c.Add(CriteriaType.Equals, "Password", Encrypt.Encode(password));

            List<Users> userlist = uHelper.QueryUsersBySearch(c, null);

            if (userlist != null && userlist.Count > 0)
            {
                Users user = userlist[0];

                Session["CurrentAdminUser"] = user;
                //写入cookie
                HttpCookie cookie = new HttpCookie("AdminUSER_COOKIE");
                cookie.Values.Add("username", user.UserName);
                cookie.Values.Add("password", user.Password);
                cookie.Expires = System.DateTime.Now.AddDays(30.0);
                HttpContext.Current.Response.Cookies.Add(cookie);

                FunctionsHelper fHelper = new FunctionsHelper();
                List<Functions> fc = fHelper.GetCurrPowerFun(user);

                FunctionItemHelper fiHelper = new FunctionItemHelper();
                List<FunctionItem> fiList = fiHelper.GetCurrPowerFunItems(user);
                FunctionItem fstItem = new FunctionItem();
                if (fc != null && fc.Count > 0)
                {
                    foreach (Functions fun in fc)
                    {
                        if (fun.ParentID == "0")
                        {
                            fstItem = fiList.Find(delegate(FunctionItem fi) { return fi.FunctionID == fun.ID && fi.IsDisplay == 1; });

                            if (fstItem != null)
                            {
                                Response.Redirect(fstItem.PageUrl);
                                break;
                            }
                            else
                            {
                                foreach (Functions subFun in fc)
                                {
                                    if (subFun.ParentID == fun.ID)
                                    {
                                        fstItem = fiList.Find(delegate(FunctionItem fi) { return fi.FunctionID == subFun.ID && fi.IsDisplay == 1; });
                                        if (fstItem != null)
                                        {
                                            Response.Redirect(fstItem.PageUrl);
                                            break;
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
            else
            {
                Tools.Alert("用户名或密码不正确！");
                return;
            }

            

        }
        #endregion
    }
}